<?php
require_once dirname(__FILE__) . '/InterfaceBase.php';
class ProtectInterface extends InterfaceBase implements IService
{
    public function __construct ($env = self::APP_ENV_TEST)
    {
        parent::__construct($env);
        $this->loadSdk("SDOServiceProtect");
        $this->loadSdk("SDOServiceAuthen");
    }
    /**
     * 开通防沉迷服务
     * @param array $args array(
     * appId=>应用ID,
	 * isSelf=>是否开启自身防沉迷|boolean
	 * cas=>array('domain'=>'域名')
	 * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function open ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $msg = '';
        /*if (SDOServiceProtect::searchApp($msg, $args['appId'], self::$ENVType[$env])) {
            $message = $this->getErrMsg(-20002001, "APP EXISTS");
        	return true;
        }*/
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $isSelf = $args['isSelf'];
        if($isSelf) {
            if (! SDOServiceProtect::addSelfProtect($msg, $appId, $sdkEnv)) {
                $this->setError(SDOServiceProtect::getErrNo(), $msg);
                return false;
            }
            $casInfo = $args['cas'];
            if (! SDOServiceAuthen::setCasDomain($msg, $appId, $casInfo['domain'], $isSelf, 
            $sdkEnv)) {
                $this->setError(SDOServiceAuthen::getErrNo(), $msg);
                return false;
            }
        } else {
            if (! SDOServiceProtect::addProtect($msg, $appId, $sdkEnv)) {
                $this->setError(SDOServiceProtect::getErrNo(), $msg);
                return false;
            }
        }
        return true;
    }
    /**
     * 关闭防沉迷服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * isSelf=>是否开启自身防沉迷|boolean
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function close ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $isSelf = $args['isSelf'];
        return true;
    }
    /**
     * 更新防沉迷服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
	 * isSelf=>是否开启自身防沉迷|boolean
     * cas=>array('domain'=>'域名')
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function update ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $msg = '';
        /*if (SDOServiceProtect::searchApp($msg, $args['appId'], self::$ENVType[$env])) {
            $message = $this->getErrMsg(-20002001, "APP EXISTS");
        	return true;
        }*/
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        if (isset($args['app'])) {
            $appInfo = $args['app'];
            if (! SDOServiceProtect::addApp($msg, $appId, $appInfo['appName'], $appInfo['test'], 
            $sdkEnv)) {
                $this->setError(SDOServiceProtect::getErrNo(), $msg);
                return false;
            }
            $casInfo = $args['cas'];
            if (! SDOServiceAuthen::setCasDomain($msg, $appId, $casInfo['domain'], $isSelf, 
            $sdkEnv)) {
                $this->setError(SDOServiceProtect::getErrNo(), $msg);
                return false;
            }
        }
        return true;
    }
    public function get ($args, $env = null)
    {}
    /**
     * 
     * @param array $message
     * @param array $args array(
     * 'appId'=>应用ID
     * )
     * @param mixed $env
     */
    public function sync ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        return true;
    }
}